<?php

namespace api\controller;

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\CommonService;
use common\service\TaskWorkOrderProblemStepCategoryService;
use think\module\Module;
use think\Request;

class TaskWorkOrderProblemStepCategoryController extends VerifyController
{
    protected $taskWorkOrderProblemStepCategoryService;

    public function __construct()
    {
        parent::__construct();
        $this->taskWorkOrderProblemStepCategoryService = TaskWorkOrderProblemStepCategoryService::getInstance();
    }


    /**
     * 添加工单问题环节
     * @param \think\Request $request
     * @return \think\Response|void
     * @throws \Throwable
     */
    public function addTaskWorkOrderProblemStepCategory(Request $request)
    {
        try {
            $res = $this->taskWorkOrderProblemStepCategoryService->addTaskWorkOrderProblemStepCategory($request->param()['data'], $request->getUnionUserId());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新
     * @param \think\Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function update(Request $request)
    {
        $param = $request->param();
        //补充默认参数
        if (!empty($param['data']['is_confirm'])) {
            $param['data']['confirm_person'] = $request->getUserId();
            // 提前检测所有的工单问题是否会为no,如果是的则拉入所有qa
            if ($param['data']['is_confirm'] == "no") {
                $this->taskWorkOrderProblemStepCategoryService->checkTaskWorkOrderProblemStepCategoryIsAllNo($param['data']['id']);
            }
        }

        $instance = CommonService::instance($request->controller());
        $instance->setCurrentModuleCode($request->moduleCode);
        $throwNoDataUpdateException = $request->param()['throw_no_data_change_exception'] ?? 'yes';
        $throwNoDataUpdateException = $throwNoDataUpdateException === 'yes';
        $resData = $instance->commonUpdate($param, Module::$moduleDictData['module_index_by_code'][$request->moduleCode], $throwNoDataUpdateException);
        $resData['is_can_confirm'] = true;
        return json(success_response('', $resData));
    }

    /**
     * 批量更新
     * @param \think\Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function batchUpdateSame(Request $request)
    {
        $param = $request->param();
        //补充默认参数
        if (!empty($param['data']['is_confirm'])) {
            $param['data']['confirm_person'] = $request->getUserId();
            // 提前检测所有的工单问题是否会为no,如果是的则拉入所有qa
            if ($param['data']['is_confirm'] == "no") {
                $this->taskWorkOrderProblemStepCategoryService->checkTaskWorkOrderProblemStepCategoryIsAllNo($param['data']['id']);
            }
        }

        $instance = CommonService::instance($request->controller());
        $instance->setCurrentModuleCode($request->moduleCode);
        $resData = $instance->commonBatchUpdateSame($param['data'], module_by_code($request->moduleCode));
        return json(success_response('', $resData));
    }


    /**
     * 删除
     * @param \think\Request $request
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        try {
            $resData = $this->taskWorkOrderProblemStepCategoryService->delete($request->param()['param']['filter']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 问题环节工单统计
     * @param \think\Request $request
     * @return \think\Response|void
     * @throws \think\Exception
     */
    public function problemStepCategoryTaskWorkOrderCount(Request $request)
    {
        try {
            $tenant_id = $request->getTenantId();
            $resData = $this->taskWorkOrderProblemStepCategoryService->problemStepCategoryTaskWorkOrderCount($request->param()['param']['filter'],$tenant_id);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

}
